<?php
/**
* @version		$Id: text.php 6138 2007-01-02 03:44:18Z eddiea $
* @package		Joomla
* @subpackage	Articles
* @copyright	Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

/**
 * Renders a calendar element
 *
 * @author 		Louis Landry
 * @package 	Joomla
 * @subpackage	Articles
 * @since		1.5
 */
class JElementTimeselector extends JElement
{
   /**
	* Element name
	* @access	protected
	* @var		string
	*/
	var	$_name = 'Date';

	function fetchElement($name, $datetime = '', $offset = '', $id = 'timeselector')
	{			
		if(!empty($datetime)){
			jimport('joomla.utilities.date');
			$jdate_obj = new JDate($datetime,0);
			$jdate_obj->setOffset($offset);
			$hour = (int) $jdate_obj->toFormat('%I');
			$minute = (int) $jdate_obj->toFormat('%M');
			$meridianselect = $jdate_obj->toFormat('%p');
		}else{
			$hour	=	'blank';
			$minute	= 	'blank';
			$meridianselect = 'AM';			
		}
		
		$meridian = array(
					0=>array('value'=>'AM','text'=>'AM'),
					1=>array('value'=>'PM','text'=>'PM')
					);		
		
		$code = JElementTimeselector::genericList(1,12,1,"params[".$id."hour]",'',$hour,'');
		$code .= JElementTimeselector::genericList(0,59,1,'params['.$id.'minute]','',$minute,'%02d');
		$code .=  JHTML::_('select.genericlist', $meridian,'params['.$id.'meridian]', '', 'value','text',$meridianselect);
		return $code;
	}//end fetchElement
	
	function genericList($start = 1,$end = 1,$inc = 1, $name, $attrib = '', $selected = '', $format = ''){
		$start   = intval( $start );
        $end     = intval( $end );
        $inc     = intval( $inc );
        $arr     = array();
		$arr[] = JHTML::_('select.option',  'blank', '--' );
		
		for ($i = $start; $i <= $end ; $i+=$inc){
            $fi = $format ? sprintf( "$format", $i ) : "$i";
            $arr[] = JHTML::_('select.option',  $fi, $fi );
        }
        return JHTML::_('select.genericlist',   $arr, $name, $attrib, 'value', 'text', $selected );
	}
}
?>
